<?php

namespace app\apj\controller;

use think\Controller;

use think\Db;

class User extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->UserModel = model('User');
        $this->FollowModel = model('Follow');
    }

    // 绑定接口
    public function follow()
    {
        if ($this->request->isPost()) {
            $userId = $this->request->param('userId', '', 'trim');
            $linkUserId = $this->request->param('linkUserId', '', 'trim');


            //这个用户已存在，那就去绑定表去查询这两个id是否绑定过
            $user = $this->FollowModel->where(['be_user_id' => $linkUserId])->find();
            if ($user) {
                $code = 200;
                return $code;
                // $this->error("QQ用户已绑定过");
                // exit;
            };

            $WXuser = $this->FollowModel->where(['user_id' => $userId])->find();
            if ($WXuser) {
                $code = 200;
                return $code;
                exit;
                // $this->error("微信用户已绑定过");
                // exit;
            };

            // 组装数据
            $data = [
                'user_id' => $userId,
                'be_user_id' => $linkUserId,
            ];

            //注册  插入数据 插入成功返回插入条数，失败返回false
            $UserStatus = $this->FollowModel->save($data);
            if ($UserStatus === FALSE) {
                return $code = 0;
                exit;
            } else {
                return $code = 1;
                exit;
            }
        }
    }

    public function followid()
    {
        if ($this->request->isPost()) {
            $userId = $this->request->param('userId', '', 'trim');

            // 根据userid来查询微信openid 为空就说明是qq用户
            $user = $this->UserModel->where(['user_id' => $userId])->value("wxopen_id");

            // 判断是否为空，为空为qq用户就去关联表查询是否有绑定微信用户
            if (is_null($user)) {
                $wxuser = $this->FollowModel->where(['be_user_id' => $userId])->find();
                // 有绑定微信用户
                if ($wxuser) {
                    $data = [
                        'userid' => $wxuser['user_id'],
                        'msg' => "有绑定微信用户，userid为绑定的微信用户id"
                    ];
                    var_dump($data);
                    exit;
                } else {
                    $data = [
                        'userid' => $userId,
                        'msg' => "无绑定微信用户"
                    ];
                    var_dump($data);
                    exit;
                }
            }
        }
    }

    public function userinfo()
    {
        // if ($this->request->isPost()) {
        $openid = $this->request->param('openid', '', 'trim');

        // 根据openid来查询用户信息
        // $user = $this->UserModel->where(['open_id' => $openid])->find();
        $user = Db::name("user")->where('open_id', $openid)->find();
        // var_dump($user);
        // exit;
        // 在根据userid来查询绑定关系
        if ($user) {

            // $wxuser = $this->FollowModel->where(['be_user_id' => $user['user_id']])->select();
            $wxuser = Db::name("follow")->where('be_user_id', $user['user_id'])->find();

            // 有绑定微信用户
            if ($wxuser) {
                // 根据绑定用户的id来查询微信用户信息
                // $user1 = $this->UserModel->where(['user_id' => $wxuser['user_id']])->select();
                $user1 = Db::name("user")->where('user_id', $wxuser['user_id'])->find();

                $data = [

                    'code' => 0,
                    'user' => $user1,
                    'msg' => "有绑定微信用户，userid为绑定的微信用户id"
                ];
                return json($data);
                exit;
            } else {
                $data = [
                    'code' => 0,
                    'user' => $user,
                    'msg' => "无绑定微信用户"
                ];
                return json($data);
                exit;
            }
        }



        // }
    }
}
